import { defineStore } from 'pinia'
import { StorageEnum } from '@/common/enums'
import { getLocalStorage, dynamicAside } from '@/utils'
import type { AsideState } from './interface'
import type { MenuResponse } from '@interface'

/**
 * 侧边栏数据中心
 */

// 查看浏览器缓存是否有数据
let initialState: any[] = getLocalStorage(StorageEnum.MENU) || []
initialState = dynamicAside(initialState)

export const useAsideStore = defineStore('asideStore', {
  state(): AsideState {
    return {
      asideList: initialState,
    }
  },

  actions: {
    // 更新侧边栏数据
    updateAsideList(payload: MenuResponse[]) {
      // 刷新浏览器缓存
      localStorage.setItem(StorageEnum.MENU, JSON.stringify(payload))
      const asideList = dynamicAside(payload)
      this.asideList = asideList
    },

    update(data: any[]) {
      this.asideList = data
      // window.localStorage.setItem(ROUTES, JSON.stringify(data))
    },
    reset() {
      this.asideList = []
    },
  },
})
